var QQMapWX = require("../../../libs/qqmap-wx-jssdk1.2/qqmap-wx-jssdk")
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 收藏状态
    stars: false,
    attractions: {},
    latitude: "",
    longitude: "",
    // 26.373389,106.622949
    strPlace: "",
    lat: "",
    lng: "",
    defaultImg: "https://yueqian-1311524869.cos.ap-chengdu.myqcloud.com/images/5.png"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'UJKBZ-JCQ63-OCQ3G-ONSE4-RHAS7-BAFWW'
    });
    //保存This指向
    var that = this
    // 获取页面可用区域宽高
    this.setData({
      windowHeight: wx.getStorageSync('windowHeight'),
      windowWidth: wx.getStorageSync('windowWidth')
    })
    // 保持搜索框与胶囊平齐
    this.setData({
      orderTopHeight: wx.getStorageSync('orderTopHeight')
    })
  },
  // 回到主页
  backToHome() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  goToDetail(event) {
    // console.log(event.currentTarget.dataset)
    wx.navigateTo({
      url: '/pages/home/attractions/detail/detail?detailData=' + encodeURIComponent(JSON.stringify(event.currentTarget.dataset.detaildata)),
    })
  },
  // 收藏
  stars(event) {
    // 还缺乏逻辑，后续需要完善
    console.log(event)
    this.setData({
      stars: !this.data.stars
    })
  },
  onChange(event) {
    console.log(event.detail)
    this.setData({
      activeNames: event.detail,
    });
  },
  onChoose(event) {
    // console.log(event.currentTarget.dataset.getimage)
    var imgurl = event.currentTarget.dataset.getimage
    wx.previewImage({
      urls: [imgurl],
      showmenu: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this
    //获取用户当前位置
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        console.log(latitude, longitude)
        //保存当前坐标
        that.data.latitude = res.latitude
        that.data.longitude = res.longitude
        that.data.strPlace = latitude + "," + longitude
        console.log("StrPlace IS:", that.data.strPlace)
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          strPlace: latitude + "," + longitude
        })
      }
    })
    // 调用接口
    qqmapsdk.search({
      keyword: '景点',
      sig: 'KnAAOQeoZtHBrTiT9PmUQi4AsdtDeQoW',
      //传入当前位置
      location: that.data.strPlace,
      success: function (res) {
        console.log(res.data);
        that.setData({
          attractions: res.data
        })
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });
  },
})